package com.xzbd.minio.controller;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Objects;

import com.xzbd.minio.vo.BucketVo;

import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.minio.MinioClient;
import io.minio.errors.ErrorResponseException;
import io.minio.errors.InsufficientDataException;
import io.minio.errors.InternalException;
import io.minio.errors.InvalidResponseException;
import io.minio.errors.ServerException;
import io.minio.errors.XmlParserException;
import io.minio.messages.Bucket;

@RestController
@RequestMapping("/bucket")
public class BucketController {

    @Autowired
    private MinioClient minioClient;

    @GetMapping("/list")
    public List<BucketVo> getAllBoukes() {
        try {
            List<Bucket> list = minioClient.listBuckets();
            if (Objects.isNull(list))
                return Lists.newArrayList();
            return BucketVo.fromBucketList(list);
        } catch (InvalidKeyException | ErrorResponseException | InsufficientDataException | InternalException
                | InvalidResponseException | NoSuchAlgorithmException | ServerException | XmlParserException
                | IOException e) {
            e.printStackTrace();
            return Lists.newArrayList();
        }

    }

}